import math

from OpenGL.GL import *

class Vec2:
    def __init__(self, x = 0, y = 0):
        if type(x) == tuple:
            self.x, self.y = x
        else:
            self.x = x
            self.y = y
        
    def __add__(self, v):
        return Vec2(self.x + v.x, self.y + v.y)
    
    def __sub__(self, v):
        return Vec2(self.x - v.x, self.y - v.y)
    
    def __mul__(self, v):
        if v.__class__ == Vec2:
            return Vec2(self.x * v.x, self.y * v.y)
        else:
            return Vec2(self.x * v, self.y * v)
        
    def __rmul__(self, v):
        if v.__class__ == Vec2:
            return Vec2(self.x * v.x, self.y * v.y)
        else:
            return Vec2(self.x * v, self.y * v)
        
    def __div__(self, v):
        if v.__class__ == Vec2:
            return Vec2(self.x / v.x, self.y / v.y)
        else:
            return Vec2(self.x / v, self.y / v)
        
    def __repr__(self):
        return "<Vec2 %s %s>" % (str(self.x),str(self.y))
    
    def dot(self, v):
        return self.x * v.x + self.y * v.y
    
    def length(self):
        return math.sqrt(self.x * self.x + self.y * self.y)
    
    def angle(self, normalize = False):
        return math.atan2(self.y,self.x)
    
    def norm(self):
        d = self.length()
        if d == 0:
            return Vec2(0,0)
        else:
            return self / d
        
    def toTuple(self):
        return self.x,self.y
        
    def vertex(self):
        glVertex2f(self.x,self.y)
        
    def translate(self):
        glTranslatef(self.x,self.y,0)
        
    def scale(self):
        glScalef(self.x,self.y,1)
        
    def project(self, v):
        vLength = v.length()
        selfLength = self.length()
        cosTh = self.dot(v) / (vLength * selfLength)
        th = math.acos(cosTh)
        return Vec2(d * cosTh, d * math.sin(th))
    
    def rotate(self, angle):
        currentAngle = self.angle()
        l = self.length()
        th = currentAngle + angle
        return Vec2(math.cos(th) * l,math.sin(th) * l)
